package network.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

//接收端，随时监听准备接收
public class UdpServer {

    public static void main(String[] args) {
        DatagramSocket socket = null;

        try {
            //1.开放数据包Socket接口
            socket = new DatagramSocket(8383);

            //2.数据格式控制，接收包
            byte[] buffer = new byte[1024];
            int len = buffer.length;
            DatagramPacket packet = new DatagramPacket(buffer, 0, len);
            socket.receive(packet);
            len = packet.getLength();
            String msg = new String(packet.getData(),0,len);
            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(msg);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket!=null) {
                socket.close();
            }
        }
    }
}
